<?php
/*
 * @Author: your name
 * @Date: 2020-12-12 16:50:31
 * @LastEditTime: 2021-01-13 10:35:10
 * @LastEditors: Please set LastEditors
 * @Description: windows2008规则
 * @FilePath: /special/application/common/netfilter/Windows2008Rule.php
 */

namespace app\common\netfilter;

use app\common\definition\InternetProtocolsDefinition;

class Windows2008Rule
{
    public $rulehead = 'netsh advfirewall firewall';//规则头部
    public $direction ;//流量方向
    public $protocols;//协议

    /**
     * @name: 状态
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:43:45
     */
    public function status()
    {
        $return = 'add rule';

        return $return;
    }

    /**
     * @name: 规则名
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:46:28
     */
    public function alias ($alias)
    {
        $return = 'name="' . $alias . '"';
        return $return;
    }

    /**
     * @name: 流量方向
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:49:12
     */
    public function direction ($direction)
    {
        $this->direction = $direction;
        $return = 'dir=';
        $return .= $direction == 1 ? 'out' : 'in';
        return $return;
    }

    /**
     * @name: 处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:50:24
     */
    public function process ($process)
    {
        $return = 'action=';
        $return .= $process == 1 ? 'allow' : 'block';

        $return .= ' enable=yes';
        return $return;
    }

    /**
     * @name: 规则所属域
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-04 14:34:50
     */
    public function scope ($scope)
    {
        $return = '';
        //如果为空默认全部应用域
        if (empty($scope))
        {
            return $return;
        }
        
        $return = 'profile=' . $scope;

        return $return;
    }

    /**
     * @name: ip地址
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:52:44
     */
    public function ip ($src_ip, $dest_ip)
    {
        $return = '';

        if($this->direction == 1)
        {
            $localip = $src_ip;
            $remoteip = $dest_ip;
        }
        else
        {
            $localip = $dest_ip;
            $remoteip = $src_ip;
        }
        

        $return .= $localip == 'any' ? '' : 'localip=' . $localip . ' ';
        $return .= $remoteip == 'any' ? '' : 'remoteip=' . $remoteip;
        
        return $return;
    }

    /**
     * @name: 端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-14 11:17:46
     */
    public function port ($src_port, $dest_port)
    {
        $return = '';

        if ( $this->protocols == InternetProtocolsDefinition::ALL_PROTOCOL)
        {
            return $return;
        }
        $dest_port = str_replace(':', '-', $dest_port);
        $src_port = str_replace(':', '-', $src_port);
        if ($this->direction == 1)
        {
            $remoteport = $dest_port;
            $localport = $src_port;
        }
        else
        {
            $remoteport = $src_port;
            $localport = $dest_port;
        }
        

        $return .= $localport == 'any' ? '' : ' localport=' . $localport;
        $return .= $remoteport == 'any' ? '' : ' remoteport=' . $remoteport . ' ';

        return $return;
    }

        /**
     * @name: 协议
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:52:04
     */
    public function protocols ($protocols)
    {
        $return = '';
        if ($this->protocols == InternetProtocolsDefinition::ALL_PROTOCOL)
        {
            return $return;
        }

        $return = 'protocol=' . $protocols;
        return $return;
    }

}